< previous page page_320 next page >

Page 320
Next, you need to create the new property, called CharAccept. You can do so either by entering the Get and Let procedures by hand, or by choosing Tools, Add Procedure, Property. The code for the CharAccept property is fairly easy to understand. When the developer needs the value of the CharAccept property, the Property Get procedure simply passes what is stored in the private variable. When the value of the CharAccept property is set, the Property Let procedure assigns one of the valid values to the private variable. The following is the code for both Property procedures:
Public Property Get CharAccept() As Integer
     CharAccept = mCharAccept
End Property

Public Property Let CharAccept(ByVal nNewValue As Integer)

       Select Case nNewValue
        Case 1 To 2
            mCharAccept = nNewValue
        Case Else
            mCharAccept = 0
       End Select

       PropertyChanged "CharAccept"

End Property
Notice the use of the PropertyChanged method. This method works in conjunction with the ReadProperties and WriteProperties events, which is discussed in the next few paragraphs.
You now need to use the InitProperties event of the user control to specify an initial value of the property:
Private Sub UserControl_InitProperties()
   mCharAccept = 0
End Sub
This code ensures that a value is set, even if the developer does not set it.
Using the PropertyBag Object
You also need to create the code for the WriteProperties and ReadProperties events to preserve the designtime settings of CharAccept. These two events use the PropertyBag object to save and retrieve the value of the CharAccept property. The PropertyBag object enables you to maintain the design environment value of CharAccept. The code for these two events, shown in Listing 14.2, is not hard to understand. What is important, however, is why the code is needed.
Listing 14.2 LIMITED.ZIPMaintaining the Property Value
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
   mCharAccept = PropBag.ReadProperty("CharAccept", 0)
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
   PropBag.WriteProperty "CharAccept", mCharAccept, 0
End Sub

 
< previous page page_320 next page >